//
//  MeInfoViewController.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/12.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "MeInfoViewController.h"
#import "RegisterViewController.h"
#import "LoginOrRegisterViewController.h"
#import "ModifyPasswordController.h"
#import "ModifyUserNameController.h"
#import "MeQrCodeViewController.h"
#import "SureView.h"
#import "MQTTSessionManager.h"
#import "DataManager.h"
#import "GlobalConst.h"
#import "MeInfoCell.h"
#import "Friend.h"

static NSString* const MeInfoCellcellIdentifier = @"MeInfoCellcellIdentifier";
@interface MeInfoViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
DataManagerRefreshDelegate
>
@property (nonatomic, strong)UITableView* m_tableView;
@property (nonatomic, strong)SureView *m_viewExitLogin;
@property (nonatomic, strong)UIView *m_viewBackground;
@property (nonatomic, strong)UIButton *m_btnModifyPwd;
@property (nonatomic, strong)UIButton *m_btnIcon;
@end
@implementation MeInfoViewController


#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    [kDataManager addFriendRefreshDelegate:self];
    [self friendRefresh:kDataManager callbackType:FriendCallbackTypeName];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - event response
- (void)modifyPwd {
    ModifyPasswordController *registerVc = [[ModifyPasswordController alloc]init];
    [self.navigationController pushViewController:registerVc animated:YES];
}


#pragma mark - DataManagerRefreshDelegate

- (void)friendRefresh:(DataManager *)manager callbackType:(FriendCallbackType)callbackType {
    if (callbackType == FriendCallbackTypeName) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.m_tableView reloadData];
        });
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else {
        return 1;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeInfoCell* cell = [tableView dequeueReusableCellWithIdentifier:MeInfoCellcellIdentifier forIndexPath:indexPath];
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.titleLabel.text = @"昵称";
        [cell.rightButton setBackgroundImage:nil forState:UIControlStateNormal];
        [cell.rightButton setTitle:[self getCurrentUserName] forState:UIControlStateNormal];
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        cell.titleLabel.text = @"我的二维码";
        [cell.rightButton setBackgroundImage:[UIImage imageNamed:@"me_qrcode_icon"] forState:UIControlStateNormal];
        [cell.rightButton setTitle:nil forState:UIControlStateNormal];
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        cell.titleLabel.text = @"修改登录密码";
        [cell.rightButton setBackgroundImage:nil forState:UIControlStateNormal];
        [cell.rightButton setTitle:nil forState:UIControlStateNormal];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        ModifyUserNameController *usernameVc = [[ModifyUserNameController alloc]init];
        [self.navigationController pushViewController:usernameVc animated:YES];
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        MeQrCodeViewController * meQrCodeVc = [[MeQrCodeViewController alloc]init];
        [self.navigationController pushViewController:meQrCodeVc animated:YES];
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        [self modifyPwd];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
#pragma mark - private method

- (void)initSubviews
{
    self.navigationItem.title = @"个人信息";
    self.m_tableView.tableFooterView = self.m_viewExitLogin;
    [self.m_viewExitLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(200);
        make.height.equalTo(55);
        make.centerX.equalTo(self.m_tableView);
    }];
    [self.view addSubview:self.m_tableView];
}

- (NSString*)getCurrentUserName {
    Friend* friend = [kDataManager fetchFriendWithApsn:kMQTTSessionManager.currentUser.apsn luid:kMQTTSessionManager.currentUser.luid];
    NSLog(@"%@",friend.name);
    return  friend.name;
}

#pragma mark - getters and setters

- (UITableView *)m_tableView
{
    if (!_m_tableView) {
        _m_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _m_tableView.backgroundColor = kBgColor;
        _m_tableView.dataSource = self;
        _m_tableView.delegate = self;
        _m_tableView.rowHeight = 50;
        _m_tableView.showsVerticalScrollIndicator = false;
        _m_tableView.showsHorizontalScrollIndicator = false;
        _m_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_m_tableView registerClass:[MeInfoCell class] forCellReuseIdentifier:MeInfoCellcellIdentifier];
    }
    return _m_tableView;

}
- (SureView *)m_viewExitLogin
{
    if (!_m_viewExitLogin) {
        _m_viewExitLogin = [[SureView alloc]init];
        _m_viewExitLogin.title = @"退出登录";
        _m_viewExitLogin.sureBtnClickBlock = ^(){
            kMQTTSessionManager.currentUser.m_strPwd = nil;
            [kMQTTSessionManager.currentUser archiver];
            [kMQTTSessionManager.xai close];
            [kDataManager.gatewayListenList removeAllObjects];
            [kDataManager.deviceListenList removeAllObjects];
            [kDataManager.linkageListenList removeAllObjects];
            [[NSNotificationCenter defaultCenter] postNotificationName:kZTExitLoginNotification object:nil];
        };
    }
    return _m_viewExitLogin;
}

- (UIButton *)m_btnModifyPwd
{
    if (!_m_btnModifyPwd) {
        _m_btnModifyPwd = [[UIButton alloc]init];
        [_m_btnModifyPwd setTitle:@"修改登录密码" forState:UIControlStateNormal];
        _m_btnModifyPwd.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_m_btnModifyPwd setTitleColor:RGBColor(51, 51, 51) forState:UIControlStateNormal];
        [_m_btnModifyPwd addTarget:self action:@selector(modifyPwd) forControlEvents:UIControlEventTouchUpInside];

    }
    return _m_btnModifyPwd;
}
- (UIView *)m_viewBackground
{
    if (!_m_viewBackground) {
        _m_viewBackground = [[UIView alloc]init];
        _m_viewBackground.backgroundColor = [UIColor whiteColor];
    }
    return _m_viewBackground;
}
- (UIButton *)m_btnIcon
{
    if (!_m_btnIcon) {
        _m_btnIcon = [[UIButton alloc]init];
        [_m_btnIcon setImage:[UIImage imageNamed:@"arrow_icon"] forState:UIControlStateNormal];
        _m_btnModifyPwd.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    }
    return _m_btnIcon;
}

@end
